# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname="msys2-w32api-runtime"
pkgver=7.0.0.5479.8db8dd5a
pkgrel=1
pkgdesc="Win32 API import libs for MSYS2 toolchain"
arch=('i686' 'x86_64')
url="https://mingw-w64.sourceforge.io/"
license=('custom')
groups=('msys2-devel')
depends=('msys2-w32api-headers')
options=('staticlibs' '!strip' '!buildflags')
makedepends=('git' 'mingw-w64-cross-gcc')
#provides=($pkgname)
#conflicts=($pkgname)
source=('mingw-w64'::'git+https://git.code.sf.net/p/mingw-w64/mingw-w64')
sha256sums=('SKIP')

MINGW_CHOST=${CARCH}-w64-mingw32

pkgver() {
  cd "${srcdir}/mingw-w64"
  local _major=$(head -n 16 mingw-w64-headers/crt/_mingw_mac.h | grep '__MINGW64_VERSION_MAJOR' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _minor=$(head -n 16 mingw-w64-headers/crt/_mingw_mac.h | grep '__MINGW64_VERSION_MINOR' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _rev=0
  printf "%s.%s.%s.%s.%s" ${_major} ${_minor} ${_rev} "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd ${srcdir}/mingw-w64
}

build() {
  cd ${srcdir}/mingw-w64/mingw-w64-crt

  local _libs=
  case ${CARCH} in
    i686)
      _libs="--enable-lib32 --disable-lib64"
    ;;
    x86_64)
      _libs="--enable-lib64 --disable-lib32"
    ;;
  esac

  ./configure \
  --prefix=/crt \
  --build=${CHOST} \
  --host=${MINGW_CHOST} \
  --enable-w32api \
  ${_libs} \
  CFLAGS='-I/usr/include/w32api' \
  CXXFLAGS='-I/usr/include/w32api' \
  CPPFLAGS='-I/usr/include/w32api'

  make
}

package() {
  cd ${srcdir}/mingw-w64/mingw-w64-crt

  make DESTDIR=${pkgdir} install
  mv ${pkgdir}/crt ${pkgdir}/usr
}
